package ee.emhi;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Hoiatused extends ListActivity {

	ProgressDialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.hoiatused);
		dialog = ProgressDialog.show(Hoiatused.this, "", 
                "Andmeid laetakse. \nPalun oota...", true);
		new DownloadFilesTask().execute();
	}

	
	
	// Shit for threads
    private class DownloadFilesTask extends AsyncTask<String, Integer, String> {
    	 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		@Override
		protected String doInBackground(String... params) {
			String xml = XMLfunctions.getXML("http://www.emhi.ee/ilma_andmed/xml/hoiatus.php");
			Document doc = null;
			
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	        try {
	        	
				DocumentBuilder db = dbf.newDocumentBuilder();
				
				InputSource is = new InputSource();
		        is.setCharacterStream(new StringReader(xml));
		        doc = db.parse(is); 
		        
			} catch (ParserConfigurationException e) {
				System.out.println("XML parse error: " + e.getMessage());
				return null;
			} catch (SAXException e) {
				System.out.println("Wrong XML file structure: " + e.getMessage());
	            return null;
			} catch (IOException e) {
				System.out.println("I/O exeption: " + e.getMessage());
				return null;
			}
	        
				NodeList nodes = doc.getElementsByTagName("warning");
							
				for (int i = 0; i < nodes.getLength(); i++) {							
					HashMap<String, String> map = new HashMap<String, String>();	
					
					Element e = (Element)nodes.item(i);
					map.put("area_est", XMLfunctions.getValue(e, "area_est"));
		        	map.put("content_est", XMLfunctions.getValue(e, "content_est"));
		        	mylist.add(map);			
				}		
		       
		        
		        
		       
				return null;
		}

        protected void onPostExecute(String result) {
        	ListAdapter adapter = new SimpleAdapter(Hoiatused.this, mylist , R.layout.hoiatus, 
                    new String[] { "area_est", "content_est"}, 
                    new int[] { R.id.hoiatus_asukoht, R.id.hoiatus });
    
        	setListAdapter(adapter);
        	
        	 final ListView lv = getListView();
		        lv.setTextFilterEnabled(true);	
		        lv.setOnItemClickListener(new OnItemClickListener() {
		        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        		
		        		@SuppressWarnings("unchecked")
						HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);	        		
		        		Toast.makeText(Hoiatused.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_LONG).show(); 

					}
				});
		        dialog.dismiss();
        }


    }
}
